<?php

	require_once APPPATH . 'controllers/base_controllers/base_controller.php';

	class New_member_sign_up extends Base_controller
	{
		public static $REGISTRATION_TEMPLATE = 'templates/registration/registration_template';		
		
		public function step1()
		{
			//Create Registration Value In Session
			$this->session->unset_userdata('registration_value');
			$this->load->model('values/registration_value');
			$registrationValue = new Registration_value();
			$this->session->set_userdata('registration_value', serialize($registrationValue));
			
			$data['title'] = 'New Member Info';
			$data['view'] = 'registration/new_members/new_member_step1_view';
			
			$this->load->view( self::$REGISTRATION_TEMPLATE, $data );
		}
		
		
		public function step1_process()
		{
			//Save To Session...
			$this->load->model('values/person_value');
			$this->load->model('values/registration_value');
			
			$mainPerson = new Person_value();
			$mainPerson->setPersonName( $this->input->post('userName') );
			$mainPerson->setPassword( $this->input->post('password') );
			$mainPerson->setFirstName( $this->input->post('firstName') );
			$mainPerson->setLastName( $this->input->post('lastName') );
			$mainPerson->setGender( $this->input->post('gender') );
			$mainPerson->setEmail1( $this->input->post('primaryEmail') );
			$mainPerson->setPhone1( $this->input->post('homePhoneNumber') );
			$mainPerson->setPhone2( $this->input->post('mobilePhoneNumber') );
			//DATE JOINED CMB GOES HERE
			$mainPerson->setAddress1( $this->input->post('homeAddress1') );
			$mainPerson->setAddress2( $this->input->post('homeAddress2') );
			$mainPerson->setCity( $this->input->post('city') );
			$mainPerson->setState( $this->input->post('state') );
			$mainPerson->setZip( $this->input->post('zipCode') );
			$mainPerson->setNumberOfChildren( $this->input->post('numberOfChildrenAttendingBalvihar') );
			
			$registrationValue = unserialize($this->session->userdata('registration_value'));
			
			$registrationValue = Registration_value::cast($registrationValue);
			$registrationValue->setMainPerson($mainPerson);
			
			$this->session->unset_userdata('registration_value');
			$this->session->set_userdata('registration_value', serialize($registrationValue));
			
			if( $this->input->post('enterSpouseInformation') == 'yes')
			{
				redirect( base_url() . 'index.php/new_member_sign_up/step2/' );
			}
			else if( $this->input->post('numberOfChildrenAttendingBalvihar') > 0 )
			{
				redirect( base_url() . 'index.php/new_member_sign_up/step3/' );
			}
			else
			{
				echo('Complete!');
			}
			
			die;
		}
		
		
		public function step2()
		{
			$data['title'] = 'New Spouse Info';
			$data['view'] = 'registration/new_members/new_member_step2_view';
			
			$this->load->view( self::$REGISTRATION_TEMPLATE, $data );
		}
		
		
		public function step2_process()
		{
			//Save To Session...
			$this->load->model('values/person_value');
			$this->load->model('values/registration_value');
			
			$spousePerson = new Person_value();
			$spousePerson->setPersonName( $this->input->post('userName') );
			$spousePerson->setPassword( $this->input->post('password') );
			$spousePerson->setFirstName( $this->input->post('firstName') );
			$spousePerson->setLastName( $this->input->post('lastName') );
			$spousePerson->setGender( $this->input->post('gender') );
			$spousePerson->setEmail1( $this->input->post('primaryEmail') );
			$spousePerson->setPhone1( $this->input->post('homePhoneNumber') );
			$spousePerson->setPhone2( $this->input->post('mobilePhoneNumber') );
			//DATE JOINED CMB GOES HERE
			
			$registrationValue = unserialize($this->session->userdata('registration_value'));
			
			$registrationValue = Registration_value::cast($registrationValue);
			$registrationValue->setSpouse($spousePerson);
			
			$this->session->unset_userdata('registration_value');
			$this->session->set_userdata('registration_value', serialize($registrationValue));
			
			if( Person_value::cast($registrationValue->getMainPerson())->getNumberOfChildren() > 0 )
			{
				redirect( base_url() . 'index.php/new_member_sign_up/step3/' );
			}
			else
			{
				echo('Complete!');
			}
			die;
		}
		
		
		public function step3()
		{
			$data['title'] = 'New Child Info';
			$data['view'] = 'registration/new_members/new_member_step3_view';
			
			$this->load->view( self::$REGISTRATION_TEMPLATE, $data );
		}
		
		
		public function step3_process()
		{
			//Save To Session...
			$this->load->model('values/person_value');
			$this->load->model('values/registration_value');
			
			$childPerson = new Person_value();
			$childPerson->setFirstName( $this->input->post('firstName') );
			$childPerson->setLastName( $this->input->post('lastName') );
			$childPerson->setGender( $this->input->post('gender') );
			$childPerson->setEmail1( $this->input->post('primaryEmail') );
			$childPerson->setPhone1( $this->input->post('homePhoneNumber') );
			$childPerson->setBirthDate( $this->input->post('birthDate') );
			//DATE JOINED CMB GOES HERE
			
			$registrationValue = unserialize($this->session->userdata('registration_value'));
			$registrationValue = Registration_value::cast($registrationValue);
			
			$childArray = $registrationValue->getChildren();
			if( $childArray == null || sizeof($childArray) == 0 )
			{
				$childArray = array();
			}
			
			array_push($childArray, $childPerson);
			$registrationValue->setChildren($childArray);
			
			$this->session->unset_userdata('registration_value');
			$this->session->set_userdata('registration_value', serialize($registrationValue));
			
			if( sizeof($registrationValue->getChildren()) < Person_value::cast($registrationValue->getMainPerson())->getNumberOfChildren() )
			{
				redirect( base_url() . 'index.php/new_member_sign_up/step3/' );
			}
			else
			{
				echo('Complete!');
			}
			
			die;
		}
		
	}

?>